import sys
import math
sys.path.append("/home/jmmauricio/Documents/APEIRON2012/ahkab/")


import ahkab
import circuit, printing, devices

N_pp = 4/2
U_s_linea = 440. # V
Z_s = 0.2 + 2.0*1j # Ohmios
Z_r = 0.4 + 2.0*1j #Ohmios
F_n=50.0
s = 0.03
R_fe = 200.0
X_mu = 40.0
n=1447.0
n_1 = 1500.0
s = (n_1-n)/n_1 # deslizamiento


mycircuit = circuit.circuit(title="Butterworth Example circuit", filename=None)

gnd = mycircuit.get_ground_node()

#mycircuit.add_inductor(name="L_mu", ext_n1=gnd, ext_n2="n3", L=X_mu/(2.0*math.pi*F_n))
#mycircuit.add_resistor(name="R_fe", ext_n1=gnd, ext_n2="n3", R=R_fe)
mycircuit.add_inductor(name="L_s", ext_n1="n1", ext_n2="n2", L=Z_s.imag/(2.0*math.pi*F_n))
mycircuit.add_resistor(name="R_s", ext_n1="n2", ext_n2="n3", R=Z_s.real)
mycircuit.add_inductor(name="L_r", ext_n1="n3", ext_n2="n4", L=Z_r.imag/(2.0*math.pi*F_n))
mycircuit.add_resistor(name="R_r", ext_n1="n4", ext_n2=gnd, R=Z_s.real/s)


voltage_step = devices.sin(vo=0, va=100., freq=50.0, td=0, theta=0.0)
mycircuit.add_vsource(name="V1", ext_n1="n1", ext_n2=gnd, vdc=0., vac=100., function=voltage_step)

printing.print_circuit(mycircuit)

    
op_analysis = {"type":"op", "guess_label":None}
ac_analysis = {"type":"ac", "start":50, "stop":50, "nsteps":1}
tran_analysis = {"type":"tran", "tstart":0, "tstop":0.2, "tstep":100e-6, "uic":0, "method":None, "ic_label":None}
r = ahkab.process_analysis(an_list=[op_analysis, ac_analysis], circ=mycircuit, outfile="script_output", verbose=2, cli_tran_method=None, guess=True, disable_step_control=False)
#r = ahkab.process_analysis(an_list=[ac_analysis], circ=mycircuit, outfile="script_output", verbose=2, disable_step_control=False)

#import pylab

##fig = pylab.figure()
##pylab.title(mycircuit.title + " - TRAN Simulation")
##pylab.plot(r['tran']['T'].T, r['tran']['VN1'].T, label="Input voltage")
##pylab.hold(True)
##pylab.plot(r['tran']['T'].T, r['tran']['VN2'].T, label="output voltage")
##pylab.legend()
##pylab.hold(False)
##pylab.grid(True)
##pylab.ylim([0,1.2])
##pylab.ylabel('Step response')
##pylab.xlabel('Time [s]')
##fig.savefig('tran_plot.png')
##
##fig = pylab.figure()
##pylab.subplot(211)
##pylab.semilogx(r['ac']['w'].T, r['ac']['|Vn4|'].T, 'o-')
##pylab.ylabel('abs(V(n4)) [V]')
##pylab.title(mycircuit.title + " - AC Simulation")
##pylab.subplot(212)
##pylab.grid(True)
##pylab.semilogx(r['ac']['w'].T, r['ac']['arg(Vn4)'].T, 'o-')
##pylab.xlabel('Angular frequency [rad/s]')
##pylab.ylabel('arg(V(n4)) [rad]')
##fig.savefig('ac_plot.png')
##pylab.show()
